//
//  ModuleManager.m
//  HttpDemo
//  
//  Created by 程三 on 17/8/6.
//  Copyright © 2017年 程三. All rights reserved.
//

#import "ModuleManager.h"

static ModuleManager *moduleManager;

@implementation ModuleManager

//+(ModuleManager *)sharedInstance
//{
//    static dispatch_once_t once;
//    dispatch_once(&once, ^{
//        if (!moduleManager)
//        {
//            moduleManager = [[ModuleManager alloc]init];
//        }
//    });
//    return moduleManager;
//}

-(void)startInitWithModleNameArray:(NSArray *)moduleNameArray application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if(moduleNameArray == nil || moduleNameArray.count <= 0)
    {
        return;
    }
    
    NSMutableArray *modules = [[NSMutableArray alloc] init];
    for (NSString *moduleName in moduleNameArray)
    {
        if(moduleName)
        {
            Class class = NSClassFromString(moduleName);
            id obj = [[class alloc] init];
            if(obj)
            {
                [modules addObject:obj];
            }
        }
        
    }
    
    SEL sel = @selector(application:didFinishLaunchingWithOptions:);
    //循环调用didFinishLaunchingWithOptions方法
    for (id<ModuleProtocol> module in modules)
    {
        //用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
        if ([module respondsToSelector:sel])
        {
            [module application:application didFinishLaunchingWithOptions:launchOptions];
        }
    }
}

- (void)startInitWithModleNameArray:(NSArray *)moduleNameArray Application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    if(moduleNameArray == nil || moduleNameArray.count <= 0)
    {
        return;
    }
    
    NSMutableArray *modules = [[NSMutableArray alloc] init];
    for (NSString *moduleName in moduleNameArray)
    {
        if(moduleName)
        {
            Class class = NSClassFromString(moduleName);
            id obj = [[class alloc] init];
            if(obj)
            {
                [modules addObject:obj];
            }
        }
        
    }
    
    SEL sel = @selector(application:openURL:options:);
    //循环调用didFinishLaunchingWithOptions方法
    for (id<ModuleProtocol> module in modules)
    {
        //用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
        if ([module respondsToSelector:sel])
        {
            [module application:app openURL:url options:options];
        }
    }
}

- (void)startInitWithModleNameArray:(NSArray *)moduleNameArray Application:(UIApplication *)app handleOpenURL:(NSURL *)url
{
    if(moduleNameArray == nil || moduleNameArray.count <= 0)
    {
        return;
    }
    
    NSMutableArray *modules = [[NSMutableArray alloc] init];
    for (NSString *moduleName in moduleNameArray)
    {
        if(moduleName)
        {
            Class class = NSClassFromString(moduleName);
            id obj = [[class alloc] init];
            if(obj)
            {
                [modules addObject:obj];
            }
        }
        
    }
    
    SEL sel = @selector(application:handleOpenURL:);
    //循环调用didFinishLaunchingWithOptions方法
    for (id<ModuleProtocol> module in modules)
    {
        //用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
        if ([module respondsToSelector:sel])
        {
            [module application:app handleOpenURL:url];
        }
    }
}


@end
